package ar.republicagroup.gestion.util.helpers;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Clase Helper para realizar Sorting en Map
 * @author DIEGOALV
 */
public class MapSorting {
    
    
     /*
     * Paramterized method to sort Map e.g. HashMap or Hashtable in Java
     * throw NullPointerException if Map contains null key
     * @see Read more: http://javarevisited.blogspot.com/2012/12/how-to-sort-hashmap-java-by-key-and-value.html#ixzz2ldpa27rP
     */
    public static <K extends Comparable,V extends Comparable> Map<String,Object> sortByKeys(Map<String,Object> map){
        List<String> keys = new LinkedList<String>(map.keySet());
        Collections.sort(keys);
     
        //LinkedHashMap will keep the keys in the order they are inserted
        //which is currently sorted on natural ordering
        Map<String,Object> sortedMap = new LinkedHashMap<String,Object>();
        for(String key: keys){
            sortedMap.put(key, map.get(key));
        }
     
        return sortedMap;
    }


}
